package me.nujoug.foctapp;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import me.nujoug.net.HttpClientHelper.HttpHelperTask;
import me.nujoug.net.dao.Hospital;
import me.nujoug.net.dao.Utils;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class HospListActivity extends FoctWebBaseActivity {
	private HospItemAdapter hospAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hosp_list);
		// Show the Up button in the action bar.
		setupActionBar();
		
		//Set links
		thisUrl = "http://www.bjguahao.gov.cn/comm/list-0-3-0-1.html";
		mTitle = (TextView)findViewById(R.id.textViewTitle);
		pb = (ProgressBar)findViewById(R.id.loadingProgressBar);
		pb.setVisibility(ProgressBar.VISIBLE);

		// Set adapter
		hospAdapter = new HospItemAdapter(this, R.layout.row_list_hosp);
		ListView listViewHosp = (ListView) findViewById(R.id.listHosp);
		listViewHosp.setAdapter(hospAdapter);
		
		new HttpHelperTask(){
			@Override
			protected void onPostExecute(Object result) {
				super.onPostExecute(result);
				handleHttpResponse(result);
			}
		}.execute(thisUrl, referer);
		

	}

	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {

		getActionBar().setDisplayHomeAsUpEnabled(true);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.hosp_list, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void onSeachHosp(View view){
		EditText seachText = (EditText) findViewById(R.id.seachHospID);
		if(seachText != null && seachText.getText() != null && !seachText.getText().toString().trim().isEmpty()){
			try {
				String searchUrl = "http://www.bjguahao.gov.cn/comm/list.php?words="
					+ URLEncoder.encode(seachText.getText().toString().trim(),   "gbk");
				pb.setVisibility(ProgressBar.VISIBLE);
				new HttpHelperTask(){
					@Override
					protected void onPostExecute(Object result) {
						super.onPostExecute(result);
						handleHttpResponse(result);
					}
				}.execute(searchUrl, thisUrl);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void handleHttpResponse(Object result) {
		String subText = Utils.getSubString(result.toString(),
				"<div class=\"yy_content\">", "<div class='page'>");
		String pattern = "<ul[^>]*><li>(.+?)</li>\\s+</ul>";
		Matcher m = Pattern.compile(pattern).matcher(subText);
		hospAdapter.clear();
		while (m.find()) {
			String hospSection = m.group(1);
			Hospital h = new Hospital();
			if (h.parseContent(hospSection))
				hospAdapter.add(h);
		}
		pb.setVisibility(ProgressBar.INVISIBLE);
	}
}
